<template>
  <div class="app-container">
    <div class="mainbox">
      <dt-table
        ref="tableRef"
        v-loading="loading"
        :table-opts="tableOpts"
        :pagination-data="{
          total:total,
          queryParams:queryParams
        }"
        @pagination="getList"
        @searchChange="handleQuery"
        @searchReset="handleReset"
        @selection-change="handleSelectionChange"
      />
    </div>
  </div>
</template>

<script>
import { listApp, unLock, unLockMore } from '@/framework/api/authenticationCenter/accountUnlocking/accountUnlocking'

export default {
  name: 'AccountUnlocking',
  dicts: ['LockStatus'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      multipleSelection: [],
      // 总条数
      total: 0,
      // 账号解锁表格数据
      AccountUnlockingList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        account: null,
        realName: null
      },
      tableOpts: {
        rowKey: 'userId',
        toolbarBtn: [
          {
            label: '批量解锁',
            permission: ['ACCOUNTUNLOCKING_MOREUNLOCK_BUTTON'],
            type: 'primary',
            click: () => {
              this.unLockMore()
            }
          }
        ],
        firstTableCol: {
          select: true,
          width: 70
        },
        indexTableCol: {
          label: '序号',
          width: 70
        },
        tableCol: [
          {
            prop: 'account',
            label: '账号',
            show: true,
            search: true
          },
          {
            prop: 'realName',
            label: '姓名',
            show: true,
            search: true
          },
          {
            prop: 'positionName',
            label: '岗位',
            show: true
          },
          {
            prop: 'phone',
            label: '电话',
            show: true
          },
          {
            prop: 'lockState',
            label: '锁定状态',
            show: true,
            type: 'dict',
            formatter: () => { return this.dict.type.LockStatus }
          },
          {
            prop: 'lockTime',
            label: '锁定时间',
            show: true
          }
        ],
        operator: {
          fixed: 'right',
          align: 'center',
          width: 96,
          operatorBtn: [
            {
              text: '解锁',
              permission: ['ACCOUNTUNLOCKING_UNLOCK_BUTTON'],
              click: (row, col, cellValue) => {
                return this.unLock(row)
              }
            }
          ]
        },
        tableData: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询账号解锁列表 */
    getList() {
      this.loading = true
      listApp(this.queryParams).then(({ data: response }) => {
        this.tableOpts.tableData = response.rows
        this.total = response.totalRows
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery(formModel) {
      this.queryParams = Object.assign(this.queryParams, formModel)
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryParams = {
        account: '',
        realName: '',
        pageNo: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    unLock(row) {
      this.$dtModal.confirm(`是否确认解锁账号为"${row.account}"的数据项？`).then(() => {
        return unLock({ userId: row.userId })
      }).then(() => {
        this.$dtMessage({
          type: 'success',
          message: '解锁成功'
        })
        this.getList()
        this.multipleSelection.forEach((item, index, multipleSelection) => {
          if (item.userId === row.userId) {
            multipleSelection.splice(index, 1)
            return multipleSelection
          } else {
            return multipleSelection
          }
        })
        if (this.multipleSelection.length == 0) {
          this.$refs.tableRef.$refs.table.clearSelection()
        }
      })
    },
    unLockMore() {
      if (!this.multipleSelection || this.multipleSelection.length === 0) {
        this.$dtMessage({
          title: '失败',
          message: '请至少选择一项',
          type: 'error',
          duration: 2000
        })
        return
      }
      const ids = this.multipleSelection.map((res) => {
        return res.userId
      }).join(',')
      unLockMore({ lockedUserIds: ids }).then(() => {
        this.$dtMessage({
          type: 'success',
          message: '批量解锁成功'
        })
        this.getList()
        this.$refs.tableRef.$refs.table.clearSelection()
      })
    }
  }
}
</script>

